package com.example.mcdonaldapplication.activity

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.widget.Toast
import com.example.mcdonaldapplication.databinding.ActivityRegisterBinding
import com.example.mcdonaldapplication.info.ServerIP
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONObject
import kotlin.concurrent.thread

class RegisterActivity : AppCompatActivity() {
    private lateinit var binding:ActivityRegisterBinding
    private val serverIp = ServerIP.getIp()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRegisterBinding.inflate(layoutInflater)
        setContentView(binding.root)
        supportActionBar?.hide()

        val btnRegister = binding.btnRegister
        btnRegister.setOnClickListener{
            val username = binding.editUsername.text.toString()
            val password = binding.editPassword.text.toString()
            val ratioMan = binding.radioMan
            val ratioWoman = binding.radioWoman
            val birthday = binding.editBirthday.text.toString()
            val phone = binding.editPhone.text.toString()
            var gender = "半男半女"
            if(ratioMan.isChecked)
                gender = "男"
            else if (ratioWoman.isChecked)
                gender = "女"
            if(username == "" || password == "")
                Toast.makeText(this,"请输入账号密码",Toast.LENGTH_SHORT).show()
            else if(birthday == "")
                Toast.makeText(this,"请输入生日",Toast.LENGTH_SHORT).show()
            else if(phone == "")
                Toast.makeText(this,"请输入手机号",Toast.LENGTH_SHORT).show()
            else
                register(username, password, birthday, phone, gender)

        }
    }

    private fun register(username:String, password:String, birthday:String, phone:String, gender:String) {
        thread {
            //post方式提交的数据
            val formBody: FormBody = FormBody.Builder()
                .add("username", username)
                .add("password", password)
                .add("mobile", phone)
                .add("birthday", birthday)
                .add("gender", gender)
                .build()
            val client = OkHttpClient()
            val request = Request.Builder()
                .url("http://${serverIp}/signUp")
                .post(formBody)
                .build()
            val response = client.newCall(request).execute()
            val responseData = response.body.string()
            parseJsonRegister(responseData, username, password, birthday, phone, gender)
        }
    }

    private fun parseJsonRegister(jsonData: String, username: String, password: String, birthday:String, phone:String, gender:String) {
        try {
            val jsonObject = JSONObject(jsonData)
            val success = jsonObject.optString("success")
            val data = jsonObject.optString("data")
            if(success == "true") {
                val editor = getSharedPreferences("user", Context.MODE_PRIVATE).edit()
                editor.putString("username", username)
                editor.putString("password", password)
                editor.putString("mobile", phone)
                editor.putString("birthday", birthday)
                editor.putString("gender", gender)
                editor.putInt("uid", data.toInt())
                editor.apply()
                finish()
            }else{
                Looper.prepare()
                Toast.makeText(this, "账号已存在", Toast.LENGTH_SHORT).show()
                Looper.loop()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}